package com.scau402.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.scau402.common.exception.ServiceException;
import com.scau402.common.utils.DateUtils;
import com.scau402.common.utils.SecurityUtils;
import com.scau402.system.domain.WmsBasicClient;
import com.scau402.system1.domain.po.WmsWarehouseStoragePO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.scau402.system.mapper.WmsStreamShopMapper;
import com.scau402.system.domain.WmsStreamShop;
import com.scau402.system.service.IWmsStreamShopService;
import net.sourceforge.pinyin4j.PinyinHelper;

/**
 * 直播店铺Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
public class WmsStreamShopServiceImpl implements IWmsStreamShopService
{
    @Autowired
    private WmsStreamShopMapper wmsStreamShopMapper;

    /**
     * 查询直播店铺
     * 
     * @param id 直播店铺主键
     * @return 直播店铺
     */
    @Override
    public WmsStreamShop selectWmsStreamShopById(Long id)
    {
        return wmsStreamShopMapper.selectWmsStreamShopById(id);
    }

    /**
     * 查询直播店铺列表
     * 
     * @param wmsStreamShop 直播店铺
     * @return 直播店铺
     */
    @Override
    public List<WmsStreamShop> selectWmsStreamShopList(WmsStreamShop wmsStreamShop)
    {
        return wmsStreamShopMapper.selectWmsStreamShopList(wmsStreamShop);
    }

    /**
     * 新增直播店铺
     * 
     * @param wmsStreamShop 直播店铺
     * @return 结果
     */
    @Override
    public int insertWmsStreamShop(WmsStreamShop wmsStreamShop)
    {
        wmsStreamShop.setCreateTime(DateUtils.getNowDate());

        if (wmsStreamShop.getShopCode().isEmpty()) {
            wmsStreamShop.setShopCode(pregenerateCode(wmsStreamShop));
        }

        // 编码是否重复
        WmsStreamShop one = wmsStreamShopMapper.selectOne(
                new LambdaQueryWrapper<WmsStreamShop>()
                        .eq(WmsStreamShop::getShopCode, wmsStreamShop.getShopCode())
                        .eq(WmsStreamShop::getTenantId, SecurityUtils.getLoginUser().getTenantId())
                        .eq(WmsStreamShop::getDelFlag, "0")
        );

        if (one != null) {
            throw new ServiceException("店铺编码已存在");
        }

        wmsStreamShop.setTenantId(SecurityUtils.getLoginUser().getTenantId());

        wmsStreamShop.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        wmsStreamShop.setCreateTime(new Date());

        wmsStreamShop.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        wmsStreamShop.setUpdateTime(new Date());
        return wmsStreamShopMapper.insertWmsStreamShop(wmsStreamShop);
    }

    // 预生成编码
    public String pregenerateCode(WmsStreamShop wmsStreamShop)
    {
        String precode = "";
        String shopPlatform = wmsStreamShop.getShopPlatform();

        switch (shopPlatform) {
            case "抖音": precode += "DY"; break;
            case "快手": precode += "KS"; break;
            case "拼多多": precode += "PDD"; break;
            case "微信视频号": precode += "TBTM"; break;
            case "今日头条": precode += "JRTT"; break;
            case "小红书": precode += "XHS"; break;
            case "百度": precode += "BD"; break;
            case "哔哩哔哩": precode += "BLBL"; break;
            case "微信小程序": precode += "WXXCX"; break;
            case "支付宝": precode += "ZFB"; break;
            case "对庄app": precode += "DZ"; break;
            case "阿里拍卖": precode += "ALPM"; break;
            case "微拍堂": precode += "WPT"; break;
            case "玩物得志": precode += "WWDZ"; break;
            case "有赞": precode += "YZ"; break;
            case "私域": precode += "SY"; break;
            case "Tik Tok": precode += "TT"; break;
            case "Facebook": precode += "FB"; break;
            case "其他": precode += "QT"; break;
        }

        precode += getPinYinHeadChar(wmsStreamShop.getShopSimpleName());

        int num = 1;
        String formattedNum = String.format("%03d", num);
        String code = precode + formattedNum;

        // 编码是否重复
        WmsStreamShop one = wmsStreamShopMapper.selectOne(
                new LambdaQueryWrapper<WmsStreamShop>()
                        .eq(WmsStreamShop::getShopCode, code)
                        .eq(WmsStreamShop::getTenantId, SecurityUtils.getLoginUser().getTenantId())
                        .eq(WmsStreamShop::getDelFlag, "0")
        );

        while (one != null) {
            num++;
            formattedNum = String.format("%03d", num);
            code = precode + formattedNum;

            one = wmsStreamShopMapper.selectOne(
                new LambdaQueryWrapper<WmsStreamShop>()
                        .eq(WmsStreamShop::getShopCode, code)
                        .eq(WmsStreamShop::getTenantId, SecurityUtils.getLoginUser().getTenantId())
                        .eq(WmsStreamShop::getDelFlag, "0")
        );
        }

        return code;
    }

    // 获得汉字拼音首字母
    public static String getPinYinHeadChar(String str) {
        if (str == null || str.trim().isEmpty()) {
            return "";
        }
        String convert = "";
        for (int j = 0; j < str.length(); j++) {
            char word = str.charAt(j);
            // 提取字符的首字母
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);
            if (pinyinArray != null) {
                convert += pinyinArray[0].charAt(0);
            } else {
                convert += word;
            }
        }
//        去除字符中包含的空格
//        convert = convert.replace(" ","");
//        字符转小写
//        convert.toLowerCase();
        return convert.toUpperCase();
    }

    /**
     * 修改直播店铺
     * 
     * @param wmsStreamShop 直播店铺
     * @return 结果
     */
    @Override
    public int updateWmsStreamShop(WmsStreamShop wmsStreamShop)
    {
        wmsStreamShop.setUpdateTime(DateUtils.getNowDate());
        return wmsStreamShopMapper.updateWmsStreamShop(wmsStreamShop);
    }

    /**
     * 批量删除直播店铺
     * 
     * @param ids 需要删除的直播店铺主键
     * @return 结果
     */
    @Override
    public int deleteWmsStreamShopByIds(Long[] ids)
    {
        return wmsStreamShopMapper.deleteWmsStreamShopByIds(ids);
    }

    /**
     * 删除直播店铺信息
     * 
     * @param id 直播店铺主键
     * @return 结果
     */
    @Override
    public int deleteWmsStreamShopById(Long id)
    {
        return wmsStreamShopMapper.deleteWmsStreamShopById(id);
    }

    @Override
    public int startOrStop(List<WmsStreamShop> wmsStreamShopList) {
        return wmsStreamShopMapper.startOrStopByIds(wmsStreamShopList);
    }
}
